package com.yiwei.hdb.api.requestInfo;

import com.yiwei.hdb.api.Api;
import com.yiwei.hdb.api.bean.CheckCodeBean;
import com.yiwei.hdb.api.bean.CodeBean;
import com.yiwei.hdb.api.bean.ResetPwdBean;
import com.yiwei.hdb.api.bean.ResetPwdNewBean;
import com.yiwei.hdb.api.callback.ForgetPwdContract;
import com.yiwei.hdb.util.Network;
import com.yiwei.hdb.util.RxLifeUtil;
import com.yiwei.hdb.util.ToastUtil;

/**
 * Created by Administrator on 2017/5/14.
 */

public class ForgetPwdRequest extends BaseRequest<ForgetPwdContract.View> implements ForgetPwdContract.Presenter<ForgetPwdContract.View> {

    public static ForgetPwdRequest create() {
        return new ForgetPwdRequest();
    }


    @Override
    public void getCheckCodeInfo(String mobile, String verify_code) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail);
            return;
        }
        mView.onRequestStart();
        CheckCodeBean bean = new CheckCodeBean();
        bean.mobile = mobile;
        bean.verify_code = verify_code;
        Api.api_service.getCheckCodeInfo(bean).compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.getCheckCodeInfoSuccess(result);
                });
    }

    @Override
    public void getCodeInfo(String type, String mobile) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail);
            return;
        }
        mView.onRequestStart();
        CodeBean bean = new CodeBean();
        bean.type = type;
        bean.mobile = mobile;
        Api.api_service.getPwdCode(bean).compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {

                    mView.getCodeInfoSuccess(result);
                });
    }

    @Override
    public void getResetPwdInfo(String mobile, String password, String verify_code) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail);
            return;
        }
        mView.onRequestStart();
        ResetPwdBean bean = new ResetPwdBean();
        bean.mobile = mobile;
        bean.password = password;
        bean.verify_code = verify_code;
        Api.api_service.getResetPwdInfo(bean).compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.getResetPwdInfoSuccess(result);
                });
    }

    @Override
    public void getUpdatePwdInfo(String password) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail);
            return;
        }
        mView.onRequestStart();
        ResetPwdNewBean bean = new ResetPwdNewBean();
        bean.password = password;
        Api.api_service.getUpdatePwdInfo(bean).compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.getUpdatePwdInfoSuccess(result);
                });
    }
}
